package com.youlai.boot.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.ProductSalesMapper;
import com.youlai.boot.system.service.ProductSalesService;
import com.youlai.boot.system.model.entity.ProductSales;
import com.youlai.boot.system.model.form.ProductSalesForm;
import com.youlai.boot.system.model.query.ProductSalesQuery;
import com.youlai.boot.system.model.vo.ProductSalesVO;
import com.youlai.boot.system.converter.ProductSalesConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 商品销售数据服务实现类
 *
 * @author xiheforever
 * @since 2025-05-12 23:29
 */
@Service
@RequiredArgsConstructor
public class ProductSalesServiceImpl extends ServiceImpl<ProductSalesMapper, ProductSales> implements ProductSalesService {

    private final ProductSalesConverter productSalesConverter;

    /**
    * 获取商品销售数据分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<ProductSalesVO>} 商品销售数据分页列表
    */
    @Override
    public IPage<ProductSalesVO> getProductSalesPage(ProductSalesQuery queryParams) {
        Page<ProductSalesVO> pageVO = this.baseMapper.getProductSalesPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取商品销售数据表单数据
     *
     * @param id 商品销售数据ID
     * @return 商品销售数据表单数据
     */
    @Override
    public ProductSalesForm getProductSalesFormData(Long id) {
        ProductSales entity = this.getById(id);
        return productSalesConverter.toForm(entity);
    }
    
    /**
     * 新增商品销售数据
     *
     * @param formData 商品销售数据表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveProductSales(ProductSalesForm formData) {
        ProductSales entity = productSalesConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新商品销售数据
     *
     * @param id   商品销售数据ID
     * @param formData 商品销售数据表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateProductSales(Long id,ProductSalesForm formData) {
        ProductSales entity = productSalesConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除商品销售数据
     *
     * @param ids 商品销售数据ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteProductSaless(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的商品销售数据数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
